﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using PowerShell.Host.PsTFS.CdmLet.PsObject;

namespace PowerShell.Host.PsTFS.CdmLet
{
    /// <summary>
    /// Permet de supprimer un utilisateur
    /// </summary>
    [Cmdlet(VerbsCommon.Remove, "ProjectMember")]
    public class Remove_ProjectMember : Cmdlet
    {
        public Remove_ProjectMember()
        {
            if (TFSProvider.s_TFSDriveInfo != null)
            {
                this.TFS = TFSProvider.s_TFSDriveInfo.TFS;
                this.ProjectName = TFSProvider.s_TFSDriveInfo.ProjectName;
            }

        }
        protected override void ProcessRecord()
        {
            if (TFS == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server TFS!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
                return;
            }
            if (ProjectName == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server ProjectName!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
                return;
            }
            List<PsGroups> lg = new List<PsGroups>();

            // Get a reference to the version control service
            VersionControlServer versionControl = PsService.VCS(TFS);
            IGroupSecurityService sec = PsService.IGSS(TFS);

            PsMethode.RemoveUserToRole(UserName, Roles, ProjectName, TFS, versionControl, sec);
        }
        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            PsLog.AddLog(null, "Remove-ProjectMember is end");
            base.EndProcessing();
        }

        [Parameter(Position = 0)]
        public TeamFoundationServer TFS { get; set; }

        [Parameter(Position = 1)]
        public string ProjectName { get; set; }

        [Parameter(Position = 2, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string UserName { get; set; }

        [Parameter(Position = 3, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Roles { get; set; } 
    }
}


